#ifdef OS_ENABLE_CSEM

//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     C O U N T I N G   S E M A P H O R E S                                                    */
/*                                                                                                              */
/****************************************************************************************************************/


// These services are used in countsem functions (see OSA.C)

#if defined(OS_ENABLE_INT_CSEM) && (OS_CSEM_SIZE > 1)
    #define __OS_CSEM_DI()       {_OS_temp = OS_DI();}
    #define __OS_CSEM_RI()       {OS_RI(_OS_temp);}
#else
    #define __OS_CSEM_DI()
    #define __OS_CSEM_RI()
#endif




//------------------------------------------------------------------------------
// Increase counting semaphore's value by 1
#define OS_SignalCSem(countsem)                                         \
    {                                                                   \
        _OS_SignalCSem(&(countsem));                                    \
    }

//------------------------------------------------------------------------------
// Set counting semaphore's value to given value
#define OS_SetCSemValue(countsem,value)                                 \
    {                                                                   \
        __OS_CSEM_DI();                                                 \
        countsem = value;                                               \
        __OS_CSEM_RI();                                                 \
    }

//------------------------------------------------------------------------------
// Accepting counting semaphore. Decrease it's value by 1.
#define OS_AcceptCSem(countsem)                                         \
    {                                                                   \
        __OS_CSEM_DI();                                                 \
        countsem--;                                                     \
        __OS_CSEM_RI();                                                 \
    }

//------------------------------------------------------------------------------
// Set counting semaphore's value to 1.
#define OS_SetCSem(countsem)    OS_SetCSemValue(countsem, 1)
#define OS_ResetCSem(countsem)  OS_SetCSemValue(countsem, 0)
#define OS_CreateCSem(countsem) OS_SetCSemValue(countsem, 0)


//------------------------------------------------------------------------------
#if (OS_CSEM_SIZE == 1) || (!defined(OS_ENABLE_INT_CSEM))

    // Check counting semaphore for non zero value
    #define OS_CheckCSem(countsem)                      (countsem)

#else

    // Check counting semaphore for non zero value
    #define OS_CheckCSem(countsem)      _OS_CheckCSem(&(countsem))

#endif
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
#ifdef OS_ENABLE_INT_CSEM

    extern void _OS_IntSignalCSem (OST_CSEM*);

    // Set counting semaphore's value to given value
    #define OS_IntSetCSemValue(countsem,value)          countsem = value

    // Set counting semaphore's value to 1
    #define OS_IntSetCSem(countsem)                     OS_IntSetCSemValue(countsem, 1)

    // Clear counting semaphore
    #define OS_IntResetCSem(countsem)                   countsem = 0

    // Check counting semaphore for non zero value
    #define OS_IntCheckCSem(countsem)                   (countsem)

    // Accept counting semaphore. Decrease it's value by 1
    #define OS_IntAcceptCSem(countsem)                  countsem--

    // Increase counting semaphore's value by 1
    #define OS_IntSignalCSem(countsem)                  _OS_IntSignalCSem(&countsem)

#endif


//------------------------------------------------------------------------------
// Wait counting semaphore
#define OS_WaitCSem(countsem)                                           \
    {                                                                   \
        _OS_WAIT_EVENT(OS_CheckCSem(countsem));                         \
        OS_AcceptCSem(countsem);                                        \
    }

//------------------------------------------------------------------------------
// Wait counting semaphore. Exit if timeout expired.
#define OS_WaitCSemTimeout(countsem, timeout)                           \
    {                                                                   \
        _OS_WAIT_EVENT_TIMEOUT(OS_CheckCSem(countsem), timeout);        \
        if (!OS_IsTimeout()) {                                          \
            OS_AcceptCSem(countsem);                                    \
        }                                                               \
    }


//------------------------------------------------------------------------------
#endif  // OS_ENABLE_CSEM
